package com.util.过滤器;

import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author: sun.hongliang
 * @create: 2019/09/17 15:32
 */
public class FilterChain<E> implements Filter<E> {

    private final List<Filter<E>> filterList = new ArrayList<>();

    public FilterChain(Filter... filters) {
        for (Filter filter : filters) {
            filterList.add(filter);
        }
    }


    public void addFilter(Filter<E> filter) {
        filterList.add(filter);
    }

    @Override
    public List<E> filter(List<E> collections) {
        if (CollectionUtils.isEmpty(collections)) {
            return Collections.emptyList();
        }
        List<E> result = collections;
        for (Filter<E> filter : filterList) {
            result = filter.filter(result);
        }
        return result;
    }
}
